package com.homework.note.Service.impl;

import com.homework.note.Model.Enum.StatusType;
import com.homework.note.Model.Enum.TrashType;
import com.homework.note.Model.Note;
import com.homework.note.Model.Notebook;
import com.homework.note.Model.Trash;
import com.homework.note.Repository.NoteRepository;
import com.homework.note.Repository.NotebookRepository;
import com.homework.note.Repository.TrashRepository;
import com.homework.note.Service.NotebookService;
import com.homework.note.Util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;
import java.util.*;

/**
 * @author htyu
 * @date 2019/3/9
 */
@Service
public class NotebookServiceImpl implements NotebookService {

    @Autowired
    NotebookRepository notebookRepository;

    @Autowired
    TrashRepository trashRepository;

    @Autowired
    NoteRepository noteRepository;

    /**
     * 新建笔记本
     *
     * @param bookName
     * @return
     * @throws Throwable
     */
    @Override
    public Notebook create(String fatherID, String bookName) throws Throwable {
        String user_id = AuthUtil.getUserId();
        Notebook notebook = new Notebook();
        if (fatherID.equals("")) {
            notebook.setFatherID("1");
        } else {
            Notebook notebook_father = notebookRepository.findById(fatherID).orElseThrow(() -> new RuntimeException("父类笔记本不存在"));
            notebook.setFatherID(fatherID);
        }
        notebook.setBookName(bookName);
        notebook.setUserID(user_id);
        notebook.setCreateTime(new Date());
        notebook.setStatus(StatusType.Normal);
        notebookRepository.save(notebook);
        return notebook;
    }


    /**
     * 获取当前笔记本下所有的笔记本和笔记
     *
     * @return
     */
    @Override
    public HashMap<String, List> getBookList(String fatherID) throws Throwable {
        String userID = AuthUtil.getUserId();
        List<Notebook> bookList;
        List<Note> noteList;
        HashMap<String, List> map = new HashMap<>();
        if(fatherID.equals("")) {
            fatherID = "1";
        }
            //bookList = notebookRepository.findAllByUserIDAndFatherIDAndStatus(userID,fatherID,StatusType.Normal);
            //map.put("list",bookList);
        //}else {
        bookList = notebookRepository.findAllByUserIDAndFatherIDAndStatus(userID,fatherID,StatusType.Normal);
        noteList = noteRepository.findAllByUserIDAndBookIDAndStatus(userID,fatherID,StatusType.Normal);
        map.put("bookList", bookList);
        map.put("noteList", noteList);
        return map;
    }


    @Override
    public String deleteBook(String bookID) {
        Notebook notebook = notebookRepository.findById(bookID).orElseThrow(() -> new RuntimeException("笔记本不存在"));
        Trash trash = new Trash();
        trash.setUserID(notebook.getUserID());
        trash.setTitle(notebook.getBookName());
        trash.setDocumentID(notebook.getId());
        trash.setType(TrashType.File);
        trash.setDeleteTime(new Date());
        trashRepository.save(trash);
        notebook.setStatus(StatusType.Delete);
        notebookRepository.save(notebook);
        return "笔记本删除成功";
    }

    @Override
    public String rename(String bookID, String name) {
        Notebook notebook = notebookRepository.findById(bookID).orElseThrow(() -> new RuntimeException("笔记本不存在"));
        notebook.setBookName(name);
        notebookRepository.save(notebook);
        return "改名成功";
    }

    @Override
    public String move(String moveID,String fatherID){
        Notebook move_notebook = notebookRepository.findById(moveID).orElseThrow(() -> new RuntimeException("笔记本不存在"));
        notebookRepository.findById(fatherID).orElseThrow(() -> new InvalidParameterException("父类笔记本不存在"));
        move_notebook.setFatherID(fatherID);
        notebookRepository.save(move_notebook);
        return "移动成功";
    }

    @Override
    public String copy(String copyID,String fatherID){
        Notebook copy_notebook = notebookRepository.findById(copyID).orElseThrow(()-> new RuntimeException("笔记本不存在"));
        notebookRepository.findById(fatherID).orElseThrow(()-> new InvalidParameterException("父类笔记本不存在"));
        Notebook new_notebook = new Notebook();
        new_notebook.setFatherID(fatherID);
        new_notebook.setBookName(copy_notebook.getBookName());
        new_notebook.setUserID(copy_notebook.getBookName());
        new_notebook.setCreateTime(new Date());
        new_notebook.setStatus(copy_notebook.getStatus());
        notebookRepository.save(new_notebook);
        return "复制成功";
    }
}
